<?php
/**
 * Created by PhpStorm.
 * User: xiong <1099446868@qq.com>
 * DateTime: 2018/11/14
 * TIME: 23:44
 */

namespace app\index\controller;

use app\index\model\Article as ArticleModel;
use think\Db;
use think\facade\Request;
use app\index\validate\ArticleValidate;



class Article extends BaseController {

    protected $beforeActionList = [
        'islogin' => ['except'=>'single,article_list']   //在操作本控制器任何方法之前，都要先执行 islogin， 除了single方法
    ];

    //文章详情页  /single/:id
    public function single($id){
        $article = ArticleModel::getArticleById($id);
        $this->assign('article', $article);

        $tags = ArticleModel::getTags();
        $this->assign('tags', $tags);
        /**评论和回复***/
        $comm = ArticleModel::comm($id);
        $this->assign('comm_count', $comm['count']);    //评论数
        unset($comm['count']);
        $this->assign('comm', $comm);
        /*************/
        return $this->fetch('article/single');
    }

    public function markdown(){
        return $this->fetch();
    }

    public function markup(){
        $request = Request::instance();
        $param = $request->param();
        $param['content'] = $param['test-editormd-html-code'];
        unset($param['test-editormd-html-code']);
        //test-editormd-html-code  实际我们需要的textarea数据
        $error = (new ArticleValidate())->gocheck();
        $return = [
            'code' => 200,
        ];
        if(!$error['istrue']){
            $return['code'] = 400;
            $return['msg'] = $error['msg'];
            $return['token'] = Request::token();
            return json($return);
        }

        $add = ArticleModel::addArticle($param);
        if(!$add){
            $return['code'] = 400;
            $return['msg'] = '内部发生错误，请稍后重试~';
            return json($return, 500);
        }else{
            return json($return);
        }
    }

    //文件上传
    public function img_up(){
        $return = [
            'success' => 1, //0表示上传失败;1表示上传成功
            'message' => "上传失败",
            'url'     => "/static/pic/public/logo.png" //上传成功时才返回
        ];
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('editormd-image-file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( '/static/pic');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            $ext = $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $savename =  $info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            $filename =  $info->getFilename();
            $return['message'] = $file;
            $return['url'] = 'http://itbus.com/static/pic/' . $savename;
            return json($file);
        }else{
            // 上传失败获取错误信息
            $error =  $file->getError();
            $return['success'] = 0;
            $return['message'] = $error;
            return json($return);
        }

    }

    public function article_list($tag = 'php'){
        $tags = ArticleModel::getTags();
        $this->assign('tags', $tags);
        $list = ArticleModel::article_list($tag);
        $this->assign('list', $list);

        $this->assign('tag', $tag);

        return $this->fetch();
    }
}